package controllers.api.v1.base;

import controllers.api.v1.auth.ApiAuth;
import controllers.license.LicenseChecker;
import facade.AppContext;
import facade.base.AdminBusinessFacade;
import facade.open.BaseJsonAPI;
import facade.open.request.base.AdminBusinessRequest;
import facade.open.response.base.AdminBusinessResponse;
import models.base.AdminBusiness;
import models.base.AdminBusinessOperate;
import models.base.AdminUser;
import models.constants.DeletedStatus;
import play.Logger;
import play.data.validation.Required;
import play.i18n.Messages;
import play.mvc.With;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * 业务功能
 */
@With({ApiAuth.class, LicenseChecker.class})
public class AdminBusinessAPI extends BaseJsonAPI {

    //加载
    public static void index() {
        List<AdminBusiness> businessList = AdminBusiness.findAllBusiness();

        renderData(businessList);
    }

    //新建
    public static void create() {
        Logger.info("LOGGER AdminBusinessAPI 0225164700002 create 创建业务-----");
        //权限验证
//        if (!checkAuth("AdminBusinessAPI", "create"))
//            renderWarnMessage(407, Messages.get("flash.warn.no-permission"));

        AdminBusinessRequest businessRequest = validateRequestBody("business", AdminBusinessRequest.class);

        Boolean exist = AdminBusiness.existByName(businessRequest.name);
        if(exist)
            renderWarnMessage(403, "名称为["+businessRequest.name+"]的业务已添加，勿重复添加！");

        AdminBusiness adminBusiness = AdminBusinessFacade.create(businessRequest);
        flash.put("notice", Messages.get("flash.notice.save-success"));
        AdminBusinessResponse businessResponse = AdminBusinessResponse.from(adminBusiness);
        renderData(businessResponse);

    }

    /**
     * 显示详情.
     */
    public static void show(@Valid @Required String id) {
        AdminBusiness adminBusiness = AdminBusiness.findOne(id);
        if (adminBusiness == null)
            renderWarnMessage(403, "业务不存在，可能已经被删除！");
        AdminBusinessResponse response = AdminBusinessResponse.from(adminBusiness);
        renderData(response);
    }

    //修改
    public static void update(@Valid @Required String id) {
        Logger.info("LOGGER AdminBusinessAPI 0225164700003 modify 修改业务-----");

//        if (!checkAuth("AdminBusinessAPI", "modfiy"))
//            renderWarnMessage(407, Messages.get("flash.warn.no-permission"));

        AdminBusinessRequest businessRequest = validateRequestBody("business", AdminBusinessRequest.class);

        if (businessRequest == null) {
            renderWarnMessage(403, Messages.get("warn.no-exists","表单参数"));
        }
        AdminBusiness adminBusiness = AdminBusiness.findOne(id);

        if (adminBusiness == null) {
            renderWarnMessage(403, Messages.get("warn.no-exists","业务"));
        }
        notFoundIfNull(adminBusiness);

        AdminBusinessFacade.update(adminBusiness, businessRequest);
        AdminBusinessResponse businessResponse = AdminBusinessResponse.from(adminBusiness);
        flash.put("notice", Messages.get("flash.notice.update-success"));


        renderData(businessResponse);

    }

    /**
     * 删除
     */
    public static void delete(@Valid @Required String id) {
        Logger.info("LOGGER AdminBusinessAPI 0225164700003 delete 删除业务-----");
        Logger.info("--------------- 删除的业务id :%s", id);

//        if (!checkAuth("AdminBusinessAPI", "delete"))
//            renderWarnMessage(407, Messages.get("flash.warn.no-permission"));

        AdminUser loginUser = AppContext.currentUser();
        Logger.info("LOGGER AdminBusinessAPI   delete   0223163700001  删除业务功能 ，操作人:%s , 操作人ID： %s", loginUser != null?loginUser.name: "" , loginUser);

        AdminBusiness business = AdminBusiness.findOne(id);
        if (business != null) {
            business.deleted = DeletedStatus.DELETED;
            business.modifyAt = new Date();
            business.modifyUser = loginUser;
            business.save();

            AdminBusinessOperate.deleteByBusinessId(business.id);

        }
        flash.put("notice", Messages.get("flash.notice.delete-success", "业务功能"));

        renderSuccMessage("删除成功！");

    }


}
